#! /usr/bin/python

import os

srcFiles = ['%s/test_files/%s' % (os.getcwd(), testFile) 
              for testFile in os.listdir('./test_files')
                   if os.path.isfile(testFile)]

def findSourceFiles():
  srcFiles.append('%s/%s' % (os.getcwd(), 'code_test.py'))

  for fileName in os.listdir('./test_files'):
    if os.path.isfile('./test_files/%s' % fileName):
      srcFiles.append('%s/test_files/%s' % (os.getcwd(), fileName))

  os.chdir('../src/')

  srcFiles.append('%s/%s' % (os.getcwd(), 'cdslib.h'))

  for folderName in os.listdir('.'):
    if os.path.isdir(folderName) and not folderName.startswith('.'):
      tempDir = os.getcwd()
      os.chdir('%s/%s' % (tempDir, folderName))
      for fileName in os.listdir('.'):
        if os.path.isfile(fileName):
          srcFiles.append('%s/%s' % (os.getcwd(), fileName))
      os.chdir(tempDir)

def styleCheck():
  lengthLines = []
  lengthFiles = []
  tabLines = []
  tabFiles = []
  lineCount = 0

  for fileName in srcFiles:
    if not fileName.split('/').pop().startswith('.'):
      inputFile = open(fileName, 'r')
      lines = inputFile.readlines()
      inputFile.close()

      lineCount += len(lines)
        
      for i, line in enumerate(lines):
        if '\t' in line:
          tabFiles.append(fileName)
          tabLines.append(i + 1)
        if len(line) > 81:
          lengthFiles.append(fileName)
          lengthLines.append(i + 1)
          break

  havePrinted = False
  for i, fileName in enumerate(lengthFiles):
    print '%s failed line length check on line %s' % (fileName.split('/').pop(), 
                                                      lengthLines[i])
    havePrinted = True
  for i, fileName in enumerate(tabFiles):
    print '%s failed tab check on line %s' % (fileName.split('/').pop(), 
                                                      tabLines[i])
    havePrinted = True

  if havePrinted:
    print ''
  print 'cdslib has currently %s lines of code' % lineCount

findSourceFiles()
styleCheck()

